package com.efounder.util;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import androidx.appcompat.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.efounder.mobilecomps.R;
import com.utilcode.util.ScreenUtils;

/**
 * Created by zhangshunyun on 2017/6/16.
 * <p>
 * 用于群打卡提示用户打开wifi gps等
 */

public class DialogUtil {
    public static void showDialog(Context context, String title, String message, String positiveBtnText, DialogInterface.OnClickListener positiveListener, String negativeBtnText,
                                  DialogInterface.OnClickListener negativeListener) {
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(context);
        normalDialog.setTitle(title);
        normalDialog.setPositiveButton(positiveBtnText, positiveListener);
        normalDialog.setNegativeButton(negativeBtnText, negativeListener);
        normalDialog.setMessage(message);
        normalDialog.show();
    }
    /**
     * title_msg_sure 带确定回调
     *
     * @param packageContext
     * @param sureListener
     * @param infoMsg
     * @param sureMsg
     */
    public static void showDialogNoCancel(Context packageContext,
                                          DialogInterface.OnClickListener sureListener, int titleId,String infoMsg, String sureMsg) {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(packageContext);
        builder.setCancelable(false);
        builder.setMessage(infoMsg);
        builder.setTitle(titleId);
        builder.setPositiveButton(sureMsg, sureListener);
        builder.create().show();
    }
    /**
     * title_msg_sure 带确定回调
     *
     * @param packageContext
     * @param sureListener
     * @param infoMsg
     * @param sureMsg
     */
    public static void showDialogWithCancle(Context packageContext,
                                          DialogInterface.OnClickListener sureListener,String title, String infoMsg, String sureMsg,String cancleMsg) {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(packageContext);
        builder.setCancelable(true);
        builder.setMessage(infoMsg);
        builder.setTitle(title);
        builder.setPositiveButton(sureMsg, sureListener);
        builder.setNegativeButton(cancleMsg, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        android.app.AlertDialog dialog = builder.create();
        dialog.show();
    }
    /**
     * 自定义对话框
     *
     * @param context
     * @param layout      填充view id
     * @param gravity     设置位置 bottom top
     * @param isFillWidth 是否横向铺满 true false
     * @return
     */
    public static Dialog customDialogWithLayout(Context context, int layout, int gravity, boolean isFillWidth) {
        final Dialog infoDialog = new Dialog(context, R.style.dialog_style);// 创建自定义样式dialog
        LayoutInflater inflater = LayoutInflater.from(context);
        // 得到加载view
        View customView = inflater.inflate(layout, null);
        infoDialog.setCancelable(true);
        infoDialog.setContentView(customView);
        Window window = infoDialog.getWindow();
        if (gravity > -1)
            window.setGravity(gravity);//默认是窗口正中
        WindowManager.LayoutParams lp = window.getAttributes();
        if (isFillWidth){
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        }else {
            // 设置屏幕的80%宽度
            lp.width = (int) (ScreenUtils.getScreenWidth() * 0.80);
        }
        window.setAttributes(lp);
        return infoDialog;
    }

}
